<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.purchaseId"
        placeholder="入库号"
        style="width: 150px"
        class="filter-item"
      />
      <el-input
        v-model="listQuery.contractId"
        placeholder="订单号"
        style="width: 150px"
        class="filter-item"
      />
      <el-input
        v-model="listQuery.storeId"
        placeholder="仓库号"
        style="width: 150px"
        class="filter-item"
      />
      <el-date-picker
        v-model="listQuery.write_date"
        type="date"
        placeholder="日期"
        style="width: 100px"
        class="filter-item"
      />
      <el-input
        v-model="listQuery.cargo"
        placeholder="货物名"
        style="width: 150px"
        class="filter-item"
      />
      <el-radio-group v-model="listQuery.sort" style="margin-bottom:10px;">
        <el-radio-button label="ASC">正序查询</el-radio-button>
        <el-radio-button label="DESC">倒序查询</el-radio-button>
      </el-radio-group>

      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleSearch"
      >Search</el-button>

      <el-button
        v-waves
        round
        class="filter-item"
        type="suceess"
        icon="el-icon-refresh"
        @click="tableReset"
      >重置</el-button>
      <el-button
        round
        class="filter-item"
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-edit"
        @click="add()"
      >新增</el-button>
      <el-button
        round
        class="filter-item"
        style="margin-left: 10px"
        type="success"
        icon="el-icon-check"
        @click="selectTruck"
      >批量出库</el-button>
    </div>

    <div>
      <el-table
        ref="multipleTable"
        border
        stripe
        :data="pageInfos.tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column
          prop="purchaseId"
          label="入库号"
          align="center"
          width="120"
          sortable
        />
        <el-table-column
          prop="contractId"
          label="订单号"
          align="center"
          width="120"
          sortable
        />
        <el-table-column
          prop="storeId"
          label="仓库号"
          align="center"
          width="120"
          sortable
        />
        <el-table-column
          prop="write_date"
          label="入库日期"
          align="center"
          width="120"
          sortable
        />
        <el-table-column
          prop="cargo"
          label="货物名"
          align="center"
          width="120"
          sortable
        />
        <el-table-column prop="status" label="货物状态" align="center" />
        <el-table-column
          label="操作"
          align="center"
          width="230"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="{ row }">
            <el-button
              round
              type="primary"
              size="mini"
              @click="handleUpdate(row)"
            >编辑</el-button>
            <el-button
              round
              type="danger"
              size="mini"
              @click="() => {}"
            >撤销入库</el-button>
            <el-button
              round
              size="mini"
              type="success"
              @click="SingleInvoice(row)"
            >出库</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="block">
      <el-pagination
        :total="pageInfos.totalCount"
        :page-size="listQuery.pageSize"
        :page-sizes="[20, 40, 60, 80, 100]"
        :pager-count="pageInfos.pagerCountDisplayLimit"
        :current-page="listQuery.pageCurrent"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleSearch"
        @size-change="handleSearch"
        @prev-click="handleSearch"
        @next-click="handleSearch"
      />
    </div>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form ref="form" :model="form" label-width="140px">
        <el-form-item label="入库号" style="width: 360px">
          <el-input v-model="form.purchaseId" />
        </el-form-item>
        <el-form-item label="订单号" style="width: 360px">
          <el-input v-model="form.contractId" />
        </el-form-item>
        <el-form-item label="仓库号" style="width: 360px">
          <el-input v-model="form.storeId" />
        </el-form-item>
        <el-form-item label="入库日期">
          <el-date-picker
            v-model="form.write_date"
            type="date"
            placeholder="选择日期"
            style="width: 55%"
          />
        </el-form-item>
        <el-form-item label="货物名" style="width: 360px">
          <el-input v-model="form.cargo" />
        </el-form-item>
        <el-form-item label="货物状态" style="width: 360px">
          <el-input v-model="form.status" />
        </el-form-item>
        <el-form-item style="center">
          <el-button type="primary" @click="onSubmit">确定</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
      <!--
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
      -->
    </el-dialog>
    <el-dialog
      title="出库"
      :visible.sync="invoiceDialogVisible"
      width="30%"
    >
      <el-form ref="form" label-width="140px">
        <el-form-item label="装配货车">
          <el-select v-model="pageInfos.truckIdSelected" placeholder="请选择车牌号">
            <el-option
              v-for="i in pageInfos.truckIds"
              :key="i.truckId"
              :label="i.truckId"
              :value="i.truckId"
            />
          </el-select>
        </el-form-item>
        <el-form-item style="center">
          <el-button type="primary" @click="handleDelete">确定</el-button>
          <el-button @click="invoiceDialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import { purchaseAdd, purchaseDelete, purchaseEdit, purchaseSearch } from '@/api/purchase'

export default {
  directives: {
    waves
  },
  data() {
    return {
      pageInfos: undefined,
      listQuery: undefined,
      dialogVisible: false,
      invoiceDialogVisible: false,
      multipleSelection: [],
      form: {
        purchaseId: '',
        contractId: '',
        storeId: '',
        write_date: '',
        cargo: '',
        status: ''
      },
      truckIds: [
        {
          truckId: 'C1231'
        },
        {
          truckId: 'C12121'
        }
      ]
    }
  },
  // mounted() {
  //   window.vue = this
  // },
  created() {
    this.tableInit()
  },
  methods: {
    add() {
      this.form.purchaseId = this.form.contractId = this.form.storeId = this.form.cargo = this.form.write_date = this.form.status =
        ''
      this.dialogVisible = true
    },
    SingleSelect(row) {
      this.$refs.multipleTable.clearSelection()
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    SingleInvoice(row) {
      this.SingleSelect(row)
      this.selectTruck()
        .then(this.$refs.multipleTable.clearSelection())
    },
    selectTruck() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          info: 'Info',
          message: '无选择货物'
        })
        return
      }
      this.pageInfos.truckIdSelected = ''
      this.invoiceDialogVisible = true
    },
    getSelectedIds() {
      const ids = []
      this.multipleSelection.forEach((element) => {
        ids.push({
          id: element.purchaseId
        })
      })
      return ids
    },
    getSelectedInfos() {
      const infos = []
      this.multipleSelection.forEach((element) => {
        infos.push({
          contractId: element.contractId,
          storeId: element.storeId,
          status: element.status,
          truckId: this.pageInfos.truckIdSelected
        })
      })
      return infos
    },
    filterReset() {
      this.listQuery = {
        purchaseId: undefined,
        contractId: undefined,
        storeId: undefined,
        write_date: undefined,
        cargo: undefined,
        pageCurrent: 1,
        pageSize: 20,
        sort: 'ASC'
      }
    },
    pageInfosReset() {
      this.pageInfos = {
        totalCount: 0,
        pagerCountDisplayLimit: 10,
        tableData: [],
        truckIds: [],
        truckIdSelected: ''
      }
    },
    tableInit() {
      this.pageInfosReset()
      this.filterReset()
      this.handleSearch()
    },
    tableReset() {
      location.reload()
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogVisible = true
      this.form = this.temp
    },
    handleDelete() {
      if (this.pageInfos.truckIdSelected === '') {
        this.$message({
          info: 'Info',
          message: '请选择货车'
        })
        return
      }

      const purchaseIds = this.getSelectedIds()
      const invoices = this.getSelectedInfos()

      this.$confirm('选中' + purchaseIds.length + '个数据，确认执行操作？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$refs.multipleTable.clearSelection()
          this.$notify({
            title: '成功',
            message: '出库成功',
            type: 'success',
            duration: 2000
          })
        })
        .catch(() => {
          this.$notify({
            title: '撤销',
            message: '撤销出库',
            type: 'danger',
            duration: 2000
          })
        })

      this.invoiceDialogVisible = false
    },
    handleSearch() {
      purchaseSearch(this.listQuery)
        .then(response => {
          this.PageInfos.totalCount = response.data.pageInfos.totalCount
          this.tableData = response.data.tableData
        })
    },
    onSubmit() {
      this.$alert('success!', 'edit', {
        confirmButtonText: '确定'
      })
    },
    hello() {
      console.log('')
      //   if(userIds!=""){
      //     this.$confirm('是否确认删除用户编号为"' + userIds + '"的数据？', "警告", {
      //       confirmButtonText: "确定",
      //       cancelButtonText: "取消",
      //       type: "warning",
      //     })
      //       .then(function () {
      //         return deleteUsers(userIds);
      //       }).then(() => {
      //           //少一次请求？减少服务器负担
      //           if (row.id) {
      //             //删除一条
      //             this.list.splice(index, 1);// 本地删除 - 本地缓存操作 减少一次 findAll
      //           } else {
      //             //forEach  this.userList.splice(index, 1);
      //             //批量删除
      //             this.getList();
      //           }
      //           this.$notify({
      //             title: "Success",
      //             message: "Delete Successfully",
      //             type: "success",
      //             duration: 2000,
      //           });
      //       })
      //       .catch(function () {});
      //   }else{
      //      this.$notify({
      //           title: "Info",
      //           message: "Select less One Row to Delete!",
      //           type: "danger",
      //           duration: 2000,
      //         });
      //   }
    }
  }
}
</script>
